package UILayer;
import CtrlLayer.*;
import ModelLayer.*;
import java.util.*;

public class OrdersUI
{
    OrderController orderController;
    public OrdersUI(){
        orderController = new OrderController();
    }

    public void ordersMenuStart(int sessionId)
    {
        boolean exit=false;
        while (!exit) 
        {
            int choice = writeOrdersMenu(sessionId);

            switch(choice){
                case 1:
                startCreateOrder(sessionId);
                break;
                case 2:
                startFindOrder();
                break;
                case 0:
                exit = true;
                break;
            }
        }
    }

    private int writeOrdersMenu(int sessionId){     
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** Orders Menu ***");
        System.out.println(" (1) Start an order*");
        System.out.println(" (2) Show an order");
        System.out.println(" (0) Back");
        System.out.print("\n Make your choice: ");
        int choice = keyboard.nextInt();
        return choice;
    }

    private void startCreateOrder(int sessionId){
        Scanner keyboard = new Scanner(System.in);
        System.out.println("###Order form:###");
        System.out.println("Enter the ID of the contractor:");
        int contractorId = Integer.parseInt(keyboard.nextLine());
        int orderId = orderController.createOrder(sessionId);
        if(orderId != 0) {
            boolean exit = false;
            while(!exit)
            {
                System.out.println(" (1) Add a product");
                System.out.println(" (2) Remove a product");
                System.out.println(" (0) end");
                int choice = Integer.parseInt(keyboard.nextLine());

                switch(choice){
                    case 1:
                    addProduct(orderId);
                    break;
                    case 2:
                    removeProduct(orderId);
                    break;
                    case 0:
                    exit = true;
                    break;
                }
            }
            if(!orderController.endOrder(orderId)) {
                System.out.println("Order with ID: " + orderId + " has been finished!");
            } else {
                System.out.println("Order can not be finished! The amount is 0!");
            }
        } else {
            System.out.println("Invalid Contractor!");
            orderController.deleteOrder(orderId);
        }
        pause();
    }
    
    private void addProduct(int orderId){
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Product barcode:");
        int productBarcode = Integer.parseInt(keyboard.nextLine());
        System.out.println("Product amount:");
        int amount = Integer.parseInt(keyboard.nextLine());
        boolean check = orderController.addProductInOrderManually(orderId, productBarcode, amount);
        if(!check) {
           System.out.println("There is no such product!");
           orderController.removeProductFromOrderManually(orderId,productBarcode,amount);
        }
    }
    
    private void removeProduct(int orderId){
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Product barcode:");
        int productBarcode = Integer.parseInt(keyboard.nextLine());
        System.out.println("Product amount:");
        int amount = Integer.parseInt(keyboard.nextLine());
        orderController.removeProductFromOrderManually(orderId, productBarcode, amount);
    }
    
    private void startFindOrder(){
        Scanner keyboard = new Scanner(System.in);
        System.out.println("ID");
        int id = Integer.parseInt(keyboard.nextLine());
        if(orderController.findOrder(id) != null){
            System.out.println();
            System.out.println("ID of order: " + orderController.findOrder(id).getId());
            System.out.println("Date of order: " + orderController.findOrder(id).getDate());
            System.out.println("-----------Products---------------" );

            for (int key : orderController.findOrder(id).getProducts().keySet()) {
                System.out.println("Product: " + key + "\t \t Amount: " + orderController.findOrder(id).getProducts().get(key));
            }
            System.out.println("-----------------------------------" );
            System.out.println("Employee: " + orderController.findOrder(id).getEmployeeID());
        } else {
            System.out.println("The order has not been found!");
        }
        pause();
    }

    private void pause(){
        Scanner keyboard = new Scanner(System.in);
        String vent = keyboard.nextLine();
    }
}
